# coding=utf-8
import arcpy
path = r'D:\WorkData\20250712四川河流\0813处理'
arcpy.env.workspace = path
arcpy.env.overwriteOutput = True
spatial_ref = arcpy.SpatialReference(4326)
riv = r'D:\WorkData\20250712四川河流\0813处理\rivs.shp'.decode('utf-8')
lp = r'D:\WorkData\20250712四川河流\0813处理\p_dis.shp'.decode('utf-8')

cs1 = arcpy.da.UpdateCursor(lp,['SHAPE@','dis','n_riv_name','n_riv_id','n_riv_lv','rivid','name'])
cs2 = arcpy.da.SearchCursor(riv,['SHAPE@','name','rivId','level'])
i = 0
for row1 in cs1:
    try:
        disList = []
        for row2 in cs2:
            dis = row1[0].distanceTo(row2[0])
            nr_name = row2[1]
            nr_id = row2[2]
            nr_lv = row2[3]
            res = [dis, nr_name, nr_id, nr_lv]
            if(row1[5] != row2[2]):
                disList.append(res)
        cs2.reset()
        disList.sort(key=lambda x: x[0])
        print i,row1[6],'流入', disList[0][1],disList[0][0],disList[0][2],disList[0][3]
        row1[1] = disList[0][0]
        row1[2] = disList[0][1]
        row1[3] = disList[0][2]
        row1[4] = disList[0][3]
        cs1.updateRow(row1)
    except:
        pass
    i += 1
